package com.bluetoothprint;

import java.io.UnsupportedEncodingException;

import android.content.Context;

public class PrintTools {
	public static final int PageWidth = 48;
	public static final int NameWidth = 20;
	public static final int PerPriceWidth = 9;
	public static final int CountWidth = 9;
	public static final int TotalPriceWidth = 10;
	private BluetoothService mService;
	private Context mContext;

	public PrintTools(BluetoothService mService, Context context) {
		this.mService = mService;
		this.mContext = context;
	}

	/**
	 * Set font gray scale
	 */
	public void fontGrayscaleSet(int ucFontGrayscale)
			throws IllegalAccessException {
		// Check that we're actually connected before trying anything
		if (mService.getState() != BluetoothService.STATE_CONNECTED) {
			throw new IllegalAccessException("Bluetooth Device not Ready");
		}
		if (ucFontGrayscale < 1)
			ucFontGrayscale = 4;
		if (ucFontGrayscale > 8)
			ucFontGrayscale = 8;
		byte[] send = new byte[3];// ESC m n
		send[0] = 0x1B;
		send[1] = 0x6D;
		send[2] = (byte) ucFontGrayscale;
		mService.write(send);
	}

	/**
	 * Sends a message.
	 * 
	 * @param message
	 *            A string of text to send.
	 * 
	 */
	public void sendMessage(String message) throws IllegalAccessException {
		// Check that we're actually connected before trying anything
		if (mService.getState() != BluetoothService.STATE_CONNECTED) {
			throw new IllegalAccessException("Bluetooth Device not Ready");
		}

		// Check that there's actually something to send
		if (message.length() > 0) {
			// Get the message bytes and tell the BluetoothService to write
			byte[] send;
			try {
				send = message.getBytes("GB2312");
			} catch (UnsupportedEncodingException e) {
				send = message.getBytes();
			}
			mService.write(send);
			//
			// // Reset out string buffer to zero and clear the edit text field
			// mOutStringBuffer.setLength(0);
			// mOutEditText.setText(mOutStringBuffer);
		}
	}
}
